import 'package:flutter/material.dart';
import '../../package/toast/BotToastCall.dart';

/*
 * @class SearchWidget
 * @author: ZhongWb
 * @date: 2019/1/20 10:55
 * @description 搜索栏
 * */
class SearchWidget extends StatefulWidget {
  String hinText;//提示
  Widget rightWidget;//右侧组件
  bool isEnabled;//输入框是否可点击
  GestureTapCallback onTapRight;//右侧点击事件，定义了rightWidget则无效
  GestureTapCallback onTapCentent;//当输入框不可点击输入时 生效
  ValueChanged<String> onSubmitted;//回车
  ValueChanged<String> onChanged;//回调
  TextEditingController controller;//控制器
  double contentPadding;
  FocusNode foncusNode;//焦点
  SearchWidget({
    this.onTapCentent,
    this.onTapRight,
    this.isEnabled:true,
    this.rightWidget,
    this.hinText:"这里是提示语",
    this.onChanged,
    this.onSubmitted,
    this.controller,
    this.foncusNode,
    this.contentPadding:9.1
});
  @override
  _SearchWidgetState createState() => _SearchWidgetState();
}

class _SearchWidgetState extends State<SearchWidget> {
  TextEditingController _controller = TextEditingController();
  FocusNode _focusNode = FocusNode();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if(widget.controller != null){
      _controller = widget.controller;
    }
    if(widget.foncusNode != null){
      _focusNode = widget.foncusNode;
    }
  }

  //组件发生更新时触发
  @override
  void didUpdateWidget(SearchWidget oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    if(widget.controller != null){
      _controller = widget.controller;
    }
    if(widget.foncusNode != null){
      _focusNode = widget.foncusNode;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 43,
      alignment: Alignment.center,
      padding: EdgeInsets.only(left: 12, right: 12,bottom: 12),
      color: Color(0xFF3D7ADD),
      child: Container(
        height: 32,
        child: Row(
          children: <Widget>[
            //输入框
            Expanded(
              child: Stack(
                children: <Widget>[
                  //输入框
                  Container(
                    padding: EdgeInsets.only(left: 35,right: 10),
                    decoration: BoxDecoration(
                        color: Color(0xFF3A63CC),
                        borderRadius: BorderRadius.circular(16)
                    ),
                    child: ConstrainedBox(
                      constraints: BoxConstraints(
                          maxHeight: 32
                      ),
                      child: InkWell(
                        child: TextField(
                          focusNode: _focusNode,
                          controller: _controller,
                          maxLines:1,
                          enabled: widget.isEnabled ?? true,
                          onChanged:(v){
                            widget.onChanged(v);
                          },
                          onSubmitted:(v){
                            widget.onSubmitted(v);
                          },
                          style: TextStyle(color: Colors.white,fontSize: 14),
                          cursorColor: Colors.white,
                          textAlign: TextAlign.left,
                          scrollPadding: EdgeInsets.all(0),
                          decoration: InputDecoration(
                            counterText: "",
                            border: InputBorder.none,
                            hintText: widget.hinText,
                            contentPadding: EdgeInsets.all(widget.contentPadding),
                            hintStyle: TextStyle(fontSize: 14,color: Colors.white),
                          ),
                        ),
                        onTap:widget.onTapCentent ??  (){
                          BotToastCall.ShowMessage("开发中",align: Alignment.center);
                        },
                      ),
                    ),
                  ),
                  //搜索图标
                  Positioned(
                    left: 10,
                    top: 7,
                    child: InkWell(
                      child: ImageIcon(AssetImage("images/package/search.png",),color: Colors.white,size: 17,),
                    ),
                  ),
                ],
              ),
            ),

            SizedBox(width: 10,),

            //右侧按钮
            widget.rightWidget ?? InkWell(
              onTap:widget.onTapRight  ??  (){
                BotToastCall.ShowMessage("开发中",align: Alignment.center);
              },
              child: Container(
                width: 79,
                height:26 ,
                decoration: BoxDecoration(
                  color: Colors.transparent,
                  border: Border.all(color: Colors.white),
                  borderRadius: BorderRadius.circular(12),
                ),
                alignment: Alignment.center,
                child: Text("更多筛选",style: TextStyle(fontSize: 13,color: Colors.white),),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
